package Unicart.applogic3;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import Unicart.applogic.ItemHelper;
import Unicart.model.Item;
import Unicart.model.User;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

public class ItemHelper3 extends DriverBasedHelper implements ItemHelper {
	
	public ItemHelper3(ApplicationManager3 manager) {
	    super(manager.getWebDriver());
	}
	
	private String savedOrderId = null;
	
	@Override
	public boolean checkProductInBasket(Item item) {
		return pages.basketPage
			.checkProduct(item.getTitle());
	}
	
	@Override
	public boolean checkProductQuantity(Item item) {
		return pages.basketPage
			.checkQuantity(item.getQuantity());
	}
	
	@Override
	public void deleteProduct(Item item) {
		pages.basketPage
			.clickButtonDeleteForProduct(item.getTitle());		
	}
	
	@Override
	public void clickButtonDeleteProductFromBasket() {
		pages.basketPage
			.clickButtonDelete();
	}

	@Override
	public void changeQuantity(Item item) {
		pages.basketPage
			.setProductQuantity(item.getQuantity());		
	}
	
	@Override
	public boolean isRecommendedBlockPresent() {
		return pages.basketPage
			.checkRecommendedBlock();
	}
	
	@Override
	public void addRecommendedProduct(Item recommendedItem) {
		pages.basketPage
			.clickOnButtonAddForRecommendedProduct(recommendedItem.getTitle());
	}
	
	@Override
	public void deleteRecommendedProductFromBasket(Item recommendedItem) {
		pages.basketPage
			.clickOnButtonDeleteForProductInBasket(recommendedItem.getTitle());
	}
	
	@Override
	public boolean productShouldBeDisplayedInRecommended(Item recommendedItem) {
		return pages.basketPage
			.checkProductInRecommendedBlock(recommendedItem.getTitle());
	}
	
	@Override
	public boolean productShouldBeDisplayedInBasket(Item recommendedItem) {
		return pages.basketPage
			.checkProductInBasketBlock(recommendedItem.getTitle());
	}

	@Override
	public boolean isCouponBlockPresent() {
		return pages.basketPage
			.checkCouponBlock();
	}

	@Override
	public void activateDiscountCoupon(Item item) {
		pages.basketPage
			.setCouponSeriesInBasket(item.getCouponSeries())
			.setCouponNumberInBasket(item.getCouponNumber())
			.clickActivateButton();		
	}

	@Override
	public boolean isCouponApplyed() {
		return pages.basketPage
			.checkCouponInBasket();
	}

	
	
	
	
	@Override
	public void selectСurrency(String value) {
		pages.basketPage
			.setOrderCurrency(value);		
	}

	@Override
	public void checkCurrency(String text) {
		pages.basketPage
			.checkCurrency(text);		
	}
	
	@Override
	public void selectRegion(User userPrivatePerson) {
		pages.basketPage
			.setRegionForDelivery(userPrivatePerson.getAddressCity());
	}
	
	@Override
	public void selectPhysicalType() {
		pages.clientDataPage
			.clickPhysicalTypeRadioButton();
	}
	
	@Override
	public void selectJuridicalType() {
		pages.clientDataPage
			.clickJuridicalTypeRadioButton();		
	}
	
	@Override
	public void fillFormForPrivatePerson(User userPrivatePerson) {
		pages.clientDataPage
			.setOrderCustomerName(userPrivatePerson.getName())
			.setOrderCustomerSurname(userPrivatePerson.getSurname())
			.setOrderCustomerEmail(userPrivatePerson.getEmail())
			.setOrderCustomerPhone(userPrivatePerson.getPhone())
			.setOrderAddressCity(userPrivatePerson.getAddressCity())
			.setOrderAddressPostalCode(userPrivatePerson.getAddressPostalCode())
			.setOrderAddressStreet(userPrivatePerson.getAddressStreet())
			.setOrderAddressHouse(userPrivatePerson.getAddressHouse())
			.setOrderAddressApartment(userPrivatePerson.getAddressApartment());
	}
	
	@Override
	public void fillFormForPrivatePersonKz(User userPrivatePerson) {
		pages.clientDataPage
			.setOrderCustomerName(userPrivatePerson.getName())
			.setOrderCustomerSurname(userPrivatePerson.getSurname())
			.setOrderCustomerEmail(userPrivatePerson.getEmail())
			.setOrderCustomerPhone(userPrivatePerson.getPhone())
			.setOrderCustomerRnn(userPrivatePerson.getKzRNN())
			.setOrderCustomerIin(userPrivatePerson.getKzIIN());	
	}
	
	@Override
	public void fillFormForLegalPerson(User userLegalPerson) {
		pages.clientDataPage
			.setOrderCustomerName(userLegalPerson.getName())
			.setOrderCustomerSurname(userLegalPerson.getSurname())
			.setOrderCustomerEmail(userLegalPerson.getEmail())
			.setOrderCustomerPhone(userLegalPerson.getPhone())
			.setOrderCompanyName(userLegalPerson.getCompanyName())
			.setOrderCompanyInn(userLegalPerson.getNumberINN())
			.setOrderCompanyKpp(userLegalPerson.getNumberKPP())
			.setOrderCompanyJuridicalAddress(userLegalPerson.getCompanyJuridicalAddress())
			.setOrderCompanyActualAddress(userLegalPerson.getCompanyActualAddress())
			.setOrderCompanyFax(userLegalPerson.getFax());
	}
	
	@Override
	public void selectOtherLicenseOwner() {
		pages.clientDataPage
			.clickOtherLicenseOwnerCheckbox();		
	}

	@Override
	public void selectOrderLicenseeTypeJuridical() {
		pages.clientDataPage
			.clickOrderLicenseeTypeJuridical();		
	}

	@Override
	public void setPaymentMethod(User userPrivatePerson) {
		pages.clientDataPage
			.setPaymentMethod(userPrivatePerson.getPaymentMethod());		
	}

	@Override
	public void clickCheckboxAgreement() {
		pages.clientDataPage
			.clickCheckboxOrderPersonalDataConfirmed();		
	}

	@Override
	public void saveOrderId() {
		savedOrderId = pages.successOrderingPage.getOrderID();		
	}
	
	@Override
	public void checkOrderId() {
		pages.orderAdminPage
			.ensurePageLoaded()
			.setIdInput(savedOrderId)
			.clickSearchButton()
			.checkId(savedOrderId);		
	}
	

	@Override
	public boolean checkErrorMessage() {
		return pages.clientDataPage
			.errorMessageIsPresent();		
	}

	@Override
	public boolean checkErrorPaymentMethodBlock() {
		return pages.clientDataPage
			.errorMessageIsPresentPaymentMethodBlock();
	}
			
}
